package org.projectname.web.admin.book;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hishenframework.core.web.menu.MenuItem;
import org.projectname.entity.book.BookPart;
import org.projectname.service.admin.book.BookPartService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * 图书分类控制器
 * @author didi
 *
 */
@Controller
@RequestMapping (BookPartController.PATH)
@MenuItem(type = "图书管理", label = "图书分类", order = 1)
public class BookPartController {
	
	public final static String PATH = "/admin/book/bookpart";
	public final static String RELOAD = "redirect:" + PATH;
	
	@Resource
	private BookPartService bookPartService;

	/**
	 * 列表
	 * @param page
	 * @return
	 */
	@RequestMapping
	public ModelAndView list() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName(PATH + "/list");
		mv.addObject("bookParts", bookPartService.list());
		return mv;
	}
	
	/**
	 * 创建
	 * @return
	 */
	@RequestMapping("/create")
	public String create() {
		return PATH + "/create";
	}
	
	/**
	 * 保存（限定提交方法只能为post方式）
	 * @param bookPartEntity 图书类别实体
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(BookPart bookPartEntity) {
//		if (1==1) {
//			return PATH + "/create";
//		}
		bookPartService.save(bookPartEntity);
		return RELOAD;
	}
	
	/**
	 * 编辑
	 * @param id 图书类别主键
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable int id) {
		ModelAndView mv = new ModelAndView(PATH + "/edit");
		mv.addObject("bookPartEntity", bookPartService.get(id));
		return mv;
	}
	
	/**
	 * 更新
	 * @param bookPartEntity 图书类别实体
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(BookPart bookPartEntity) {
		bookPartService.update(bookPartEntity);
		return new ModelAndView(RELOAD);
	}
	
	/**
	 * 删除
	 * @param id 主键
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable int id) {
		bookPartService.delete(id);
		return RELOAD;
	}
}
